(function () {
    var span = document.getElementById('layer-count'),
        stage = drawingPad.stage();

    setInterval(function () {
        span.innerHTML = "";
        stage.getChildren().forEach(function (layer) {
            span.innerHTML += [layer.getZIndex(), '. <b>' + (layer.getListening() ? '<u>' : '') + layer.attrs.name + (layer.getListening() ? '</u>' : '') + '</b> :', layer.getChildren().length , '<br />'].join('');
        });
    }, 100);
})();

window.onresize = (function () {
    drawingPad.resize();
}.throttle(250));

window.onerror = function () {
    console.log(arguments);
    alert(arguments);

};

(function ()  {
    var action, ul = document.querySelector('#actions ul');

    function pushAction(display, click) {
        var li = document.createElement('li'),
            a  = document.createElement('a');
        a.onclick = click;
        a.className = "btn action";
        a.innerText = a.text = display;
        li.appendChild(a);
        ul.appendChild(li);
    }

    pushAction('Move', function() {
        drawingPad.moveFigure();
    });

    for (action in py.Actions) {
        if (action.indexOf('Draw') == 0 && action != 'DrawAction') {
            (function(action) {
                var display = action.replace(/[A-Z]|\d/g, function($1) {return ' '+$1}),
                    methodName = 'd' + action.substring(1);
                drawingPad.registerAction(methodName, action);
                pushAction(display, function() {
                    drawingPad[methodName]();
                });
            })(action);
        }
    }
})();
